package com.zegome.game.gomestory.main.scene.play.board;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.zegome.game.handler.IUpdateHandler;

public class InputProcessor {
	
	Board board;
	
	public InputProcessor(Board board)
	{
		this.board = board;
	}
	
	Vector3 touch = new Vector3();
	
	void configPosition(final Vector3 vector, final float x, final float y) {
		vector.set(x, y, 0);
		board.screen.getCamera().unproject(vector);
	}
	
	public void touchProcessor() {		
		float xTouch, yTouch;
		int row, col;
		
		if (Gdx.input.justTouched()) 
		{			
			configPosition(touch, Gdx.input.getX(), Gdx.input.getY());			
			xTouch = touch.x;
			yTouch = touch.y;	
//			board.screen.ctr.androidHelper.log(xTouch+"---"+yTouch);
			if(xTouch>=board.dataMap.xBegin 
					&& xTouch <= board.dataMap.xBegin+ board.dataMap.wi1Pikachu*board.dataMap.maxCol
					&& yTouch >= board.dataMap.yBegin
					&& yTouch <= board.dataMap.yBegin + board.dataMap.hi1Pikachu*board.dataMap.maxRow
							){
					row = (int) ((yTouch - board.dataMap.yBegin)/board.dataMap.hi1Pikachu);
					col = (int) ((xTouch - board.dataMap.xBegin)/board.dataMap.wi1Pikachu);		
					if(board.dataMap.map[row][col].type != -1 && board.dataMap.map[row][col].isMove == false)						
					{
						if(board.dataMap.stylePlay == 9 && board.dataMap.total == board.dataMap.totalPikaFix/2){}
						else{
						board.selectProcessor.click(board.dataMap.map[row][col]);
						}
					}
					}
		}
		else if(Gdx.input.isTouched())
		{	
			
		}

	}
	
	public void init()
	{
		board.screen.registerUpdateHandler(new IUpdateHandler() {
			
			@Override
			public void reset() {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onUpdate(float delta) {
				// TODO Auto-generated method stub
				if(board.screen.ctr.a.isHasDialog() == true)
					return;
				touchProcessor();
			}

			@Override
			public boolean isDetachSelf() {
				// TODO Auto-generated method stub
				return false;
			}
		});
	}
	
}
